<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
	'id'=>'config-form',
	'enableAjaxValidation'=>false,
)); ?>
	<p class="note">Fields with <span class="required">*</span> are required.</p>
	<?php //echo $form->errorSummary($model); ?>
    <table id="Config-basic-info" class="form-view">
            <tr class="odd">
                <th><?php echo $form->labelEx($model,'group_id'); ?></th>
                <td>
                    <?php
                        echo $form->dropDownList($model, 'group_id', ConfigGroup::dumpAllGroupToArray(Yii::t('BackEnd', '-- All --')), array());
                    ?>
                    <?php echo $form->error($model,'group_id'); ?>
                </td>
            </tr>
            <tr class="even">
                <th><?php echo $form->labelEx($model,'name'); ?></th>
                <td>
                    <?php //echo $model->name;
                         echo $form->textField($model,'name',array('size'=>45,'maxlength'=>45)); ?>
                    <?php echo $form->error($model,'name'); ?>
                </td>
            </tr>
            <tr class="odd">
                <th><?php echo $form->labelEx($model,'html_tag'); ?></th>
                <td>
                    <?php
                         echo Common::getHtmlTagName($model->html_tag);
//                        echo $form->dropDownList($model, 'html_tag',
//                                 Common::dumpAllHtmlTagToArray(),
//                                 array('onChange'=>'changeValueForm(this.value)'));
                    ?>
                    <?php echo $form->error($model,'html_tag'); ?>
                </td>
            </tr>
            <tr class="even">
                <th><?php echo $form->labelEx($model,'value'); ?></th>
                <td>
                    <?php
                        if($model->html_tag == 'textField'){
                            echo $form->textField($model,'value',array('size'=>45,'maxlength'=>300));
                        }
                        elseif($model->html_tag == 'dateField'){
                                $cs=Yii::app()->clientScript;
                                $cs->registerScriptFile(Yii::app()->request->baseUrl."/js/jquery_ui/jquery-ui-1.8.6.custom.min.js", CClientScript::POS_HEAD);
                                $cs->registerCssFile(Yii::app()->request->baseUrl."/js/jquery_ui/jquery.ui.theme.css");
                                $cs->registerCssFile(Yii::app()->request->baseUrl."/js/jquery_ui/jquery.ui.core.css");
                                $cs->registerCssFile(Yii::app()->request->baseUrl."/js/jquery_ui/jquery.ui.datepicker.css");
                                $cs->registerScriptFile(Yii::app()->request->baseUrl."/js/jquery_ui/jquery.ui.core.min.js", CClientScript::POS_END);
                                $cs->registerScriptFile(Yii::app()->request->baseUrl."/js/jquery_ui/jquery.ui.datepicker.min.js", CClientScript::POS_END);
                                //if(Yii::app()->language == "vi_vn") $cs->registerScriptFile(Yii::app()->request->baseUrl."/js/jquery_ui/i18n/jquery.ui.datepicker-vi.js", CClientScript::POS_END);
                                $cs->registerScript("dateFieldEdit",'
                                    $("#Config_value").datepicker({
                                        dateFormat: "dd-mm-yy",
                                        //minDate:0,
                                        //maxDate:"+1m",
                                        buttonImage: baseUrl+"/css/admin/images/calendar.gif",
                                        showOn: "both",
                                        changeYear:true,
                                        changeMonth:true,
                                        showAnim: "slideDown"
                                    });

                                ', CClientScript::POS_READY);
                            echo $form->textField($model,'value',array('size'=>45,'maxlength'=>300, 'readOnly'=>'readOnly'));
                        }
                        else if($model->html_tag == 'textArea'){
                            echo $form->textArea($model, 'value',array('rows'=>6, 'cols'=>50));
                        }else if($model->html_tag == 'dropDownList'){
                            echo $form->dropDownList($model, 'value', ConfigOptionValue::dumpAllValueToArray($model->id));
                        }else if($model->html_tag == 'radioList'){
                            echo $form->radioButtonList($model, 'value', ConfigOptionValue::dumpAllValueToArray($model->id));
                        }
                    ?>

                    <?php if($model->html_tag == 'dropDownList' || $model->html_tag == 'radioList') {?>
                     | <a href="javascript:void(0);" onclick="javascript:$('#option-list').toggle();">
                    <?php echo Yii::t('BackEnd', 'Click here to update values list'); ?></a>
                    <?php }?>
                    
                    <?php echo $form->error($model,'value'); ?>
                </td>
            </tr>
            <tr class="odd">
                <th>&nbsp;</th>
                <td>
                    <div id="option-list" class="hide">
                        <div id="mimiTool">
                        <img onclick="addValue();" title="<?php echo Yii::t('BackEnd', 'Add');?>" alt="<?php echo Yii::t('BackEnd', 'Add');?>" src="<?php echo Yii::app()->baseUrl?>/css/admin/images/icons/add_16x16.png" />
                        <?php echo Yii::t('BackEnd', '|');?>
                        <img onclick="removeValue();" title="<?php echo Yii::t('BackEnd', 'Remove');?>" alt="<?php echo Yii::t('BackEnd', 'Remove');?>" src="<?php echo Yii::app()->baseUrl?>/css/admin/images/icons/remove_16x16.png" />
                        </div>
                        
                        <?php
                        $valueNumber = count($currentValues); $i=1;
                        foreach($currentValues as $value=>$label){?>
                        <div id="value-option-<?php echo $i;?>" class="item-value">
                            <span><?php echo Yii::t('BackEnd', 'Label'). " $i";?></span>
                            <input size="15" value="<?php echo $label;?>" name="labels[]"/>
                            <span><?php echo Yii::t('BackEnd', 'Value'). " $i";?></span>
                            <input size="15" value="<?php echo $value;?>" name="values[]"/>
                            <span><a href="javascript:void(0);" onclick="deleteConfigValue('value-option-<?php echo $i;?>','<?php echo $model->id;?>', '<?php echo $value ?>');"> <?php echo Yii::t('BackEnd', 'Delete')?> </a></span>
                        </div>
                        <?php $i++; }?>
                        <input type="hidden" value="<?php echo $valueNumber; ?>" id="valueNumber" name="valueNumber"/>
                    </div>
                </td>
            </tr>
    </table>
    <input type="submit" style="display:none"/>
<?php $this->endWidget(); ?>

</div><!-- form -->

<script type="text/javascript">
var valueNumber = document.getElementById('valueNumber').value;
function addValue()
{
    valueNumber++;
    document.getElementById('valueNumber').value  = valueNumber;

    var valueHtml ="";
    valueHtml += '<div id="value-option-'+valueNumber+'" class="item-value">';
    valueHtml += '<span><?php echo Yii::t('BackEnd', 'Label');?>  '+valueNumber+' </span>';
    valueHtml += '<input size="15" name="labels[]"/>';
    valueHtml += '<span><?php echo Yii::t('BackEnd', 'Value');?>  '+valueNumber+' </span>';
    valueHtml += '<input size="15" name="values[]"/>';
    valueHtml += '</div>';
    $('#option-list').append(valueHtml);
}
function removeValue()
{
    if(valueNumber > <?php echo $valueNumber; ?>)
    {
        $('#value-option-'+valueNumber).remove();
        valueNumber--;
        document.getElementById('valueNumber').value  = valueNumber;
    }
}
function deleteConfigValue(elementId, configId, configValue)
{
//    alert(configId+"|"+configValue);
    $('#option-list').addClass('loading');
    $.ajax({
        type: "POST",
        url: basePath+'/config/deleteConfigValue',
        data:{
            configId:configId,
            configValue:configValue,
            ajax:true,
            YII_CSRF_TOKEN:YII_CSRF_TOKEN
        },
        success: function(data){
            $('#'+elementId).remove();
            $('#option-list').removeClass('loading');
        }
    });
}
</script>
<style type="text/css">
    #mimiTool{margin: 10px 0px 5px 0px; width: 50%;}
    #mimiTool img {cursor: pointer; padding: 0px 5px 0px 5px}
    #option-list .item-value input{height:20px;background-color: #FFFFCC;}
    #Config-basic-info.form-view tr.even td a label{cursor: pointer;}
</style>